<?php
/**
 * This file is view of manager images page
 * @package Edit Image Color
 */
?>

<?php

function posts_where_media($where) {
    global $wpdb;
    $search = $_REQUEST['s'];
    $where.= " AND " . $wpdb->posts . ".post_title LIKE '%$search%'";
    return $where;
}

class List_Image_Table extends WP_List_Table {

    function __construct() {
        parent::__construct(
                array(
                    'singular' => 'wp_list_image', //Singular label
                    'plural' => 'wp_list_images', //plural label, also this well be one of the table css class
                    'ajax' => false //We won't support Ajax for this table
                )
        );
    }

    /*
     * create header and footer of table
     */

    public function get_columns() {
        return $columns = array(
            'file' => __('Id'),
            'image' => __(''),
            'title' => __('Title'),
            'author' => __('Author'),
            'uploadedto' => __('Uploaded To')
        );
    }

    /*
     * prepare the table with parameters, pagination,columns
     */

    function prepare_items($search = NULL) {
        $orderby = "ID";
        $order = "DESC";
        if (isset($_REQUEST['orderby'])) {
            $orderby = $_REQUEST['orderby'];
        }
        if (isset($_REQUEST['order'])) {
            $order = $_REQUEST['order'];
        }
        if ($search != NULL) {
            // Trim Search Term
            $search = trim($search);
            add_filter('posts_where', 'posts_where_media');
            $query_images_args = array(
                'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
            );
        } else {
            $query_images_args = array(
                'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => -1, 'orderby' => '' . $orderby, 'order' => '' . $order
            );
        }

        $query_images = new WP_Query($query_images_args);
        remove_filter('posts_where', 'posts_where_media');
        $images = array();
        foreach ($query_images->posts as $image) {
            $row = array();
            $row['id'] = $image->ID;
            $row['file'] = $image->guid;
            $row['title'] = $image->post_title;
            $author_data = get_userdata($image->post_author);
            $row['author'] = $author_data->user_nicename;
            $row['uploadedto'] = get_the_title($image->post_parent);
            $row['post_id'] = $image->post_parent;
            $image_ext = explode('.', $row['file']);
            $row['ext'] = $image_ext[count($image_ext) - 1];
            $images[] = $row;
        }
        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);
        $per_page = 5;
        $current_page = $this->get_pagenum();
        $total_items = count($images);

        // only ncessary because we have sample data
        $this->found_data = array_slice($images, (($current_page - 1) * $per_page), $per_page);

        $this->set_pagination_args(array(
            'total_items' => $total_items, //WE have to calculate the total number of items
            'per_page' => $per_page //WE have to determine how many items to show on a page
        ));
        $this->items = $this->found_data;
        //usort($this->items, array(&$this, 'usort_reorder'));
    }

    function usort_reorder($a, $b) {
        // If no sort, default to id
        $orderby = (!empty($_GET['orderby'])) ? $_GET['orderby'] : 'id';
        // If no order, default to asc
        $order = (!empty($_GET['order'])) ? $_GET['order'] : 'desc';
        // Determine sort order
        $result = strcmp($a[$orderby], $b[$orderby]);
        // Send final sort direction to usort
        return ($order === 'asc') ? $result : -$result;
    }

    function get_sortable_columns() {
        $sortable_columns = array(
            'file' => array('ID', false),
            'image' => array('image', false),
            'title' => array('title', false),
            'author' => array('author', false),
            'uploadedto' => array('uploadedto', false)
        );
        return $sortable_columns;
    }

    function column_default($item, $column_name) {

        switch ($column_name) {
            case 'file':
                return $item['id'];
            case 'image':
                $image_thumbnail = wp_get_attachment_image_src($item["id"], 'thumbnail');
                return '<a href="' . admin_url('admin.php?page=wp_plugin_edit_image_color&attachment_id=' . $item['id']) . '">' . '<img  alt="' . $item['title'] . '" class="attachment-80x60" src="' . $image_thumbnail[0] . '">' . '</a>';
            case 'title':
                $dataTitle = '<strong>' . '<a href="' . admin_url('admin.php?page=wp_plugin_edit_image_color&attachment_id=' . $item['id']) . '">' . $item['title'] . '</a>' . '</strong>';
                $dataTitle = $dataTitle . '<p>' . strtoupper($item['ext']) . '</p>';
                return $dataTitle;
            case 'author':
                return $item[$column_name];
            case 'uploadedto':
                return $item['post_id'] ? '<a href="' . admin_url('post.php?post=' . $item['post_id'] . '&action=edit') . '">' . $item['uploadedto'] . '</a>' : '(Unattached)';
            default:
                return;
        }
    }

}
?>
<?php screen_icon('upload'); ?>
<h2>Edit image color</h2>
<p style="float: left">Click an image to create new color.</p>
<br/>
<?php
$wp_list_img = new List_Image_Table();
if (isset($_POST['s'])) {
    $wp_list_img->prepare_items($_POST['s']);
} else {
    $wp_list_img->prepare_items();
}
?>

<div class="wrap">
    <form method="post">
        <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>">
        <?php
        $wp_list_img->search_box('Search', 'title');
        ?>
        <?php
        $wp_list_img->display();
        ?>
    </form>
</div>'
